package cn.gok.homework;

import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Homework5 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        writeToFile();
//        readFromFile();
//        sort1();
//        sort2();
        Pet p1=new Pet("大头猫",4);
        Pet p2=new Pet("贪吃蛇",0);
        Pet p3=new Pet("无敌狗",4);
        Pet p4=new Pet("淘气猫",4);
        Pet p5=new Pet("害羞鸡",2);
        List<Pet> pet1s=Arrays.asList(p1,p2,p3);
        List<Pet> pet2s=Arrays.asList(p4,p5);
        Family f1=new Family("张三",pet1s);
        Family f2=new Family("李四",pet2s);
        switchPets(f1,f2);

    }
    private static void writeToFile() throws IOException {
        ObjectOutputStream oos=new ObjectOutputStream(
                new FileOutputStream("day5//1.dat")
        );
        School s1=new School("胜利小学","陕西渭南白水",1779);
        School s2=new School("东湖中学","福建莆田秀屿",1992);
        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.close();
        System.out.println("写入成功");
    }
    private static void readFromFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(
                new FileInputStream("day5//1.dat")
        );
        try {
            while (true){
                Object obj=ois.readObject();
                System.out.println(obj);
            }
        } catch (EOFException e) {
            System.out.println("文件读取结束！");
        }
        ois.close();
    }
    private static void sort1(){
        List<Student> list= Arrays.asList(new Student(3,"小明",18),
                new Student(2,"闪电",16),new Student(3,"卡女",17));
        Collections.sort(list);
        for(Student s:list){
            System.out.println(s);
        }
    }
    private static void sort2(){
        List<Student> list= Arrays.asList(new Student(3,"小明",18),
                new Student(2,"闪电",16),new Student(3,"卡女",17));
        Collections.sort(list,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //先按照学号升序排序
                int result = o1.getSid()- o2.getSid();
                if (result == 0) {
                    //学号相同再按照年龄升序排序
                    result = o1.getAge() - o2.getAge();
                }
                return result;
            }
        });
        for(Student s:list){
            System.out.println(s);
        }
    }
    private static void switchPets(Family f1,Family f2){
        System.out.println("交换之前：");
        System.out.println(f1);
        System.out.println(f2);
        System.out.println("------------------------------");
//        f1中脚最多的宠物：
        Pet f1Max = f1.getPets().stream().max(Comparator.comparing(Pet::getFeetCount)).get();
//        f1中脚最多的宠物：
        Pet f2Max = f2.getPets().stream().max(Comparator.comparing(Pet::getFeetCount)).get();
        Collections.replaceAll(f1.getPets(),f1Max,f2Max);
        Collections.replaceAll(f2.getPets(),f2Max,f1Max);
        System.out.println("交换之后：");
        System.out.println(f1);
        System.out.println(f2);
    }

}
/**
 * 1 创建类 School类：name,address,  birthYear成立的年份：int （2002；2010）
 *    实现序列化接口，void writeToFile();//采用对象流写入将两个school对象写入到 1.dat中，
 *    void readFromFile();//从1.dat中读取数据到对象，并显示出来
 */
class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }
}
/**
 * 2 创建类 Student：  int sid(学号) String name(姓名) ,int age;
 * 要求使用两种方法实现自定义比较规则： 先按学号排，如果学号学号重复，按年龄排。
 * 并遍历输出结果
 */
class Student implements Comparable{
    private int sid;
    private String name;
    private int age;

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int getSid() {
        return sid;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public int compareTo(Object o) {
        Student student = (Student) o;
        //先按照学号升序排序
        int result = this.sid - student.sid;
        if (result == 0) {
            //学号相同再按照年龄升序排序
            result = this.age - student.age;
        }
        return result;
    }
}
/**
 * 3  【提高】创建类 Pet: String name(宠物的名字) ,int feetCount（脚的个数）
 *   设计类Family：String owner（主人），此外，每个家庭可以养有多只宠物（使用什么样的集合去存储宠物？？）。
 *   a 创建2个family对象，分别拥有2只和3只宠物
 *   b 写静态方法  ：   public static switchPets(Family f1,Famliy f2):  将两个家庭 脚最多的宠物进行互换。并输出互换后的结果。
 *     特别注意：如果有多只宠物的脚条数一样，任选一只进行调换即可。
 */
class Pet{
    private String name;
    private int feetCount;

    public Pet(String name, int feetCount) {
        this.name = name;
        this.feetCount = feetCount;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", feetCount=" + feetCount +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getFeetCount() {
        return feetCount;
    }

    public void setFeetCount(int feetCount) {
        this.feetCount = feetCount;
    }
}
class Family{
    private String owner;
    private List<Pet> pets;

    public Family(String owner, List<Pet> pets) {
        this.owner = owner;
        this.pets = pets;
    }

    @Override
    public String toString() {
        return "Family{" +
                "owner='" + owner + '\'' +
                ", pets=" + pets +
                '}';
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public List<Pet> getPets() {
        return pets;
    }

    public void setPets(List<Pet> pets) {
        this.pets = pets;
    }
}
